{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "99490810-4a4a-4735-afcd-6cbeeccb47e4",
   "metadata": {},
   "source": [
    "# Pandas数据分组聚合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "bfc6b313-1c9f-4a66-990f-44ce952ec088",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ab6885c-ebd7-48dc-b1cb-de796b90c092",
   "metadata": {},
   "source": [
    "数据聚合是数据处理的最后一步,通常是要使每一个数组生成一个单一的数值"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a6ff10c-05a1-4c22-aa62-6a31db6f4ae2",
   "metadata": {},
   "source": [
    "数据分类处理：\n",
    "- 分组：先把数据分为几组\n",
    "- 用函数处理：为不同组的数据应用不同的函数以转换数据\n",
    "- 合并：把不同组得到的结果合并起来"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91214fe4-5e67-4b11-9991-4d9d6f1cf170",
   "metadata": {},
   "source": [
    "数据分类处理核心：groupby()函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "1ecff821-cf48-441b-87af-edbcc8811351",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>green</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>green</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>yellow</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>blue</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>blue</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>yellow</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>yellow</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    color  price\n",
       "0   green      4\n",
       "1   green      5\n",
       "2  yellow      3\n",
       "3    blue      2\n",
       "4    blue      1\n",
       "5  yellow      7\n",
       "6  yellow      6"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "    'color':['green','green','yellow','blue','blue','yellow','yellow'],\n",
    "    'price':[4,5,3,2,1,7,6]\n",
    "})\n",
    "\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "06b86ad6-109d-48a9-92eb-bbbcaaee0af3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'blue': [3, 4], 'green': [0, 1], 'yellow': [2, 5, 6]}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按color列分组\n",
    "df.groupby(by='color')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d25a045-831c-4887-82c9-8dcb9136710a",
   "metadata": {},
   "source": [
    "使用.groups属性查看各行的分组情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "573ec498-b6b6-4d5b-9904-e85c665301ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'blue': [3, 4], 'green': [0, 1], 'yellow': [2, 5, 6]}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.groupby(by='color').groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "8415a570-616e-471f-a4f3-3bc58f8bac29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>color</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>blue</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>green</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>yellow</th>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        price\n",
       "color        \n",
       "blue        2\n",
       "green       2\n",
       "yellow      3"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分组 + 聚合\n",
    "df.groupby('color').sum()\n",
    "df.groupby('color').count()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
